package com.winning.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.framework.basedata.WebConstant;
import com.framework.helper.ServerSQLExecutorUtils;
import com.framework.server.json.JSONArray;
import com.framework.server.json.JSONObject;
import com.framework.server.manager.DBInfoManager;
import com.framework.server.manager.SQLConfigLoader;
import com.winning.common.Constans;
import com.winning.core.service.inter.LoginManagerServiceInter;

import flex.messaging.util.StringUtils;

/**
 * 类名称：LoginManagerService
 * 类描述：用户登录管理
 * 创建人：liuwensheng
 * 创建时间：2015-3-4 上午10:46:06
 * 修改人：liuwensheng
 * 修改时间：2015-3-4 上午10:46:06
 * 修改备注：
 */
@Service(value = "com.winning.sjhy.service.impl.LoginManagerService")
public class LoginManagerService implements LoginManagerServiceInter {
    private static Logger LOGGER = Logger.getLogger(Constans.LOG4J_PBS_LOGGER_ID);

    /**
     * @param @param  userName
     * @param @param  password
     * @param @return
     * @return Map<String   ,       Object>
     * @throws
     * @Title: userLogin
     * @Description: 用户登录查询
     */
    public Map<String, Object> userLogin(String userName, String password) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        String userLoginSQL = SQLConfigLoader.getSQLByID("SJHY_QUERY_USER_LOGIN");

        String userLoginCheckSQL = SQLConfigLoader.getSQLByID("SJHY_QUERY_USER_LOGIN_CHECK");

        List<HashMap<String, Object>> rs = null;

        List<HashMap<String, Object>> rsCheck = null;

        try {
            Object[] params = new Object[]{userName, password};

            //rs = ServerSQLExecutorUtils.executeQuery(userLoginSQL, params);
            rs = ServerSQLExecutorUtils.executeQueryOtherDB(userLoginSQL, params, "PT_STORE_Dev");

            if (rs == null) {
                //rsCheck = ServerSQLExecutorUtils.executeQuery(userLoginCheckSQL, params);
                rsCheck = ServerSQLExecutorUtils.executeQueryOtherDB(userLoginCheckSQL, params, "PT_STORE_Dev");

                if (rsCheck != null && rsCheck.size() > 0) {
                    resultMap.put("status", "false");
                    resultMap.put("msg", "该用户没有操作权限，请与管理员联系！");
                } else {
                    resultMap.put("status", "false");
                    resultMap.put("msg", "用户名与密码不匹配！");
                }

                return resultMap;
            } else {
                if (rs.size() > 0) {
                    HashMap<String, Object> rsMap = rs.get(0);

                    String jgbm = (String) rsMap.get("JGBM");
                    String jgmc = (String) rsMap.get("JGMC");
                    String jgjc = (String) rsMap.get("JGJC");
                    String gxqy = (String) rsMap.get("GXQY");
                    String jgjb = (String) rsMap.get("JGJB");
                    String jglb = (String) rsMap.get("JGLB");
                    String zclsh = (String) rsMap.get("ZCLSH");
                    String ryxm = (String) rsMap.get("RYXM");
                    String rybm = (String) rsMap.get("RYBM");
                    String jsmc = (String) rsMap.get("JSMC");

                    //获取当前请求线程对应的数据库产品名称
                    String dbName = DBInfoManager.getDBInfo();

                    if (WebConstant.SQLSERVER_DB.equals(dbName)) {
                        //String dayOffset = ((Integer)rsMap.get("DAYOFFSET")).toString();
                        String dayOffset = "";
                        resultMap.put("dayOffset", dayOffset);
                    }

                    resultMap.put("status", "true");

                    resultMap.put("jgbm", jgbm);
                    resultMap.put("jgmc", jgmc);
                    resultMap.put("jgjc", jgjc);
                    resultMap.put("gxqy", gxqy);
                    resultMap.put("jgjb", jgjb);
                    resultMap.put("jglb", jglb);
                    resultMap.put("zclsh", zclsh);
                    resultMap.put("ryxm", ryxm);
                    resultMap.put("rybm", rybm);
                    resultMap.put("jsmc", jsmc);
                } else {
                    //rsCheck = ServerSQLExecutorUtils.executeQuery(userLoginCheckSQL, params);
                    List<HashMap<String, Object>> rsCheck2 = ServerSQLExecutorUtils.executeQueryOtherDB(userLoginCheckSQL, params, "PT_STORE_Dev");

                    if (rsCheck2 != null && rsCheck2.size() > 0) {
                        resultMap.put("status", "false");
                        resultMap.put("msg", "该用户没有操作权限，请与管理员联系！");
                    } else {
                        resultMap.put("status", "false");
                        resultMap.put("msg", "用户名与密码不匹配！");
                    }
                }

                return resultMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return resultMap;
        }
    }

    public JSONArray getFirstMenuListHandler(Map<String, Object> paramsMap) {
        JSONArray firstMenuJsonArray = new JSONArray();

        List<HashMap<String, Object>> rs = null;

        try {
            rs = queryMenu(paramsMap);
            int i = 0;

            for (int index = 0; rs != null && index < rs.size(); index++) {
                ++i;

                HashMap<String, Object> rsMap = rs.get(index);

                JSONObject menuJsonObject = new JSONObject();

                if (rs.size() - 1 != index) {
                    menuJsonObject.put("cddm", (String) rsMap.get("cddm".toUpperCase()));
                    menuJsonObject.put("cdmc", (String) rsMap.get("cdmc".toUpperCase()));
                    menuJsonObject.put("tblj", (String) rsMap.get("tblj".toUpperCase()));
                    menuJsonObject.put("path", (String) rsMap.get("path".toUpperCase()));
                    menuJsonObject.put("cdsx", i);
                } else {
                    menuJsonObject.put("cddm", (String) rsMap.get("cddm".toUpperCase()));
                    menuJsonObject.put("cdmc", (String) rsMap.get("cdmc".toUpperCase()));
                    menuJsonObject.put("tblj", (String) rsMap.get("tblj".toUpperCase()));
                    menuJsonObject.put("path", (String) rsMap.get("path".toUpperCase()));
                    menuJsonObject.put("cdsx", i);
                }

                /**
                 if(!rs.isLast())
                 {
                 menuJsonObject.put("cddm", rs.getString("cddm"));
                 menuJsonObject.put("cdmc", rs.getString("cdmc"));
                 menuJsonObject.put("tblj", rs.getString("tblj"));
                 menuJsonObject.put("path", rs.getString("path"));
                 menuJsonObject.put("cdsx", i);
                 }
                 else
                 {
                 menuJsonObject.put("cddm", rs.getString("cddm"));
                 menuJsonObject.put("cdmc", rs.getString("cdmc"));
                 menuJsonObject.put("tblj", rs.getString("tblj"));
                 menuJsonObject.put("path", rs.getString("path"));
                 menuJsonObject.put("cdsx", i);
                 }
                 **/

                //System.out.println("========================tblj:" +menuJsonObject.getString("tblj") );

                paramsMap = new HashMap<String, Object>();

                firstMenuJsonArray.put(menuJsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return firstMenuJsonArray;
    }

    public JSONArray getSecondMenuListHandler(Map<String, Object> paramsMap, String urlParams, String zclsh, String jgbm) {
        JSONArray menuJsonArray = new JSONArray();

        List<HashMap<String, Object>> secondRS = null;
        List<HashMap<String, Object>> thirdRS = null;
        try {
            secondRS = queryMenu(paramsMap);
            int i = 0;

            for (int index = 0; secondRS != null && index < secondRS.size(); index++) {
                HashMap<String, Object> secondRSMap = secondRS.get(index);

                ++i;

                //二级菜单数据对象
                JSONObject secondMenuJsonObject = new JSONObject();

                String urlParams2 = urlParams;
                String path = (String) secondRSMap.get("path".toUpperCase());

                if (path != null) {
                    if (path.indexOf("?") > -1) {
                        if (path.indexOf("zclsh=") > -1 || path.indexOf("CAMPassword=") > -1) {
                            urlParams2 = "";
                        } else {
                            urlParams2 = "&" + urlParams2;
                        }
                    } else {
                        urlParams2 = "?" + urlParams2;
                    }
                }

                //if(!secondRS.isLast())
                if (secondRS.size() - 1 != index) {
                    secondMenuJsonObject.put("cddm", (String) secondRSMap.get("cddm".toUpperCase()));
                    secondMenuJsonObject.put("cdmc", (String) secondRSMap.get("cdmc".toUpperCase()));
                    secondMenuJsonObject.put("tblj", (String) secondRSMap.get("tblj".toUpperCase()));
                    secondMenuJsonObject.put("path", (String) secondRSMap.get("path".toUpperCase()) + urlParams2);
                    secondMenuJsonObject.put("cdsx", i);
                } else {
                    secondMenuJsonObject.put("cddm", (String) secondRSMap.get("cddm".toUpperCase()));
                    secondMenuJsonObject.put("cdmc", (String) secondRSMap.get("cdmc".toUpperCase()));
                    secondMenuJsonObject.put("tblj", (String) secondRSMap.get("tblj".toUpperCase()));
                    secondMenuJsonObject.put("path", (String) secondRSMap.get("path".toUpperCase()) + urlParams2);
                    secondMenuJsonObject.put("cdsx", i);
                }

                paramsMap = new HashMap<String, Object>();

                paramsMap.put("sjcddm", (String) secondRSMap.get("cddm".toUpperCase()));
                paramsMap.put("zclsh", zclsh);
                paramsMap.put("yljgdm", jgbm);

                int num = 0;

                //查询三级菜单
                thirdRS = queryMenu(paramsMap);

                JSONArray jsonArray3 = new JSONArray();

                for (int index_j = 0; thirdRS != null && index_j < thirdRS.size(); index_j++) {
                    HashMap<String, Object> thirdRSMap = thirdRS.get(index_j);

                    ++num;

                    JSONObject jsonObject3 = new JSONObject();

                    String urlParams3 = urlParams;
                    String path3 = (String) thirdRSMap.get("path".toUpperCase());

                    if (path3 != null) {
                        if (path3.indexOf("?") > -1) {
                            if (path3.indexOf("zclsh=") > -1 || path3.indexOf("CAMPassword=") > -1) {
                                urlParams3 = "";
                            } else {
                                urlParams3 = "&" + urlParams3;
                            }
                        } else {
                            urlParams3 = "?" + urlParams3;
                        }
                    }

                    //if(!thirdRS.isLast())
                    if (thirdRS.size() - 1 != index_j) {
                        jsonObject3.put("cddm", (String) thirdRSMap.get("cddm".toUpperCase()));
                        jsonObject3.put("cdmc", (String) thirdRSMap.get("cdmc".toUpperCase()));
                        jsonObject3.put("tblj", (String) thirdRSMap.get("tblj".toUpperCase()));
                        jsonObject3.put("path", (String) thirdRSMap.get("path".toUpperCase()) + urlParams3);
                        jsonObject3.put("cdsx", num);
                    } else {
                        jsonObject3.put("cddm", (String) thirdRSMap.get("cddm".toUpperCase()));
                        jsonObject3.put("cdmc", (String) thirdRSMap.get("cdmc".toUpperCase()));
                        jsonObject3.put("tblj", (String) thirdRSMap.get("tblj".toUpperCase()));
                        jsonObject3.put("path", (String) thirdRSMap.get("path".toUpperCase()) + urlParams3);
                        jsonObject3.put("cdsx", num);
                    }

                    jsonArray3.put(jsonObject3);

                }

                secondMenuJsonObject.put("childs", jsonArray3);

                menuJsonArray.put(secondMenuJsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return menuJsonArray;
    }


    /**
     * 查询系统框架菜单
     *
     * @param map
     * @return
     */
    public List<HashMap<String, Object>> queryMenu(Map<String, Object> map) {

        String sjcddm = (String) map.get("sjcddm");
        String ywdm = (String) map.get("ywdm");
        String zclsh = (String) map.get("zclsh");
        String yljgdm = (String) map.get("yljgdm");
        String cdjb = (String) map.get("cdjb");

        String sql = "select distinct a.cddm,a.cdmc,a.ywdm,a.sjcddm,a.sjcdmc,a.path,a.tblj,a.cdjb,a.cdsx,a.yzjd,a.wbcsbz,a.JZFS" +
                " from SYS_XTCD a ,sys_czjs_qx b,sys_czry_js c,sys_czry d,sys_zcjg_qx e" +
                " where a.jlzt='0' and d.jlzt='0' and a.cddm=b.cddm and b.jsdm=c.jsdm" +
                " and c.zclsh=d.zclsh and c.jgbm=d.jgbm" +
                " and d.jgbm=e.jgbm and b.cddm=e.cddm" +
                " and d.zclsh='" + zclsh + "' and d.jgbm='" + yljgdm + "'";

        if (!StringUtils.isEmpty(sjcddm)) {
            sql += " and a.sjcddm='" + sjcddm + "'";
        }

        if (!StringUtils.isEmpty(ywdm)) {
            sql += " and a.ywdm='" + ywdm + "'";
        }

        if (!StringUtils.isEmpty(cdjb)) {
            sql += " and a.cdjb='" + cdjb + "'";
        }

        sql += " order by cdsx";
        List<HashMap<String, Object>> rs = null;

        try {
            //rs = ServerSQLExecutorUtils.executeQuery(sql);

            //不带参数查询，则直接将参数设置为null
            rs = ServerSQLExecutorUtils.executeQueryOtherDB(sql, null, "PT_STORE_Dev");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rs;
    }

}
